
	*Preamble;
		# d cr
		clear 
		set more off 
		clear matrix
		clear mata
		set mem 700m 
		set matsize 2000 
		set maxvar 32000
		cap log close
		# d ;

	*Set working directory;
		cd "C:\Users\Lorenzo Casaburi\Documents\InventoryCredit\3ie_report_analysis";

	*Local date of most recent databases;
		local data_date 20130625;
		
	*Local tab_date;
		local tab_date 20130625;
		
	*Call database;
		use dta/inventory_credit_final_data_`data_date', replace;		

	**Baseline covariate analysis;
		*First define variables you want to plot summary statistics;
			local X_base 			
				/*SECTION B*/
				B_b_income1_farming
				B_b_trading	
				B_b_gender_hh_head
				/*SECTION C*/
				B_c_PO_importance1
				/*SECTION D*/
				B_d_num_farms
				B_d_size_palm_oil_total
				/*SECTION E*/
				B_e1_masanakay_any
				B_e2d_red_any
				B_e_qty_oil_peak
				B_e_qty_oil_offpeak
				B_e_frac_sale_by_jun
				B_e_frac_sale_by_sep
				B_e_advance10
				B_e_buy_for_sale
							
				/*SECTION G*/
				B_g2a_mas 
				B_g2b_mas
				B_g2c_mas
				B_g4a_red
				B_g4b_red
				B_g4c_red
				
				/*SECTION H*/
				B_h_CE
				
				/*SECTION I*/
				B_i_disc_0
				B_i_disc_2
				B_i_present_biased
				
				/*SECTION J*/
				B_j_try_bank
				B_j_try_informal
				B_j_borrowed_bank
				B_j_borrowed_informal
				B_j_loan_request_fail				
									
				/*SECTION K*/
				B_k2_osusu
				B_k6_save_50K	
				
				/*SECTION U*/
				B_u_asset_score
				
				/*DISTANCES*/
				B_near_mkt
				B_near_road;			

		*Basic summary stat for these variables;
		local run 1;
		if `run'==1{;
			*Adjust label;
				foreach var of varlist `X_base'{;
					local lab:variable label `var';
					local lab=subinstr("`lab'","Baseline:","",.);
					label var `var' "`lab'";
				};
			*Set empty "matrix";
				preserve;
					clear;
					set obs 100;
					*J=variable title column+# mean&sd columns+# p-values columns+obs N column;
					local J=8;
					forvalues i=1/`J'{;
						gen v`i'="";
					};
					tempfile sum_stats;
					sa `sum_stats';
				restore;
			*Fill rows for each baseline covariate;	
				local j=1;
				foreach var of varlist `X_base' {;		
					local k=`j'+1;
					local var_label: variable label `var';				
					*Mean and sd by treatment group;
					foreach group in CONTROL STORAGE ICS{;
						sum `var' if `group'==1;
						local mean_`group'=r(mean);		
						local mean_`group'= substr("`mean_`group''", 1, 5);
						dis "`mean_`group''";	
						sum `var' if `group'==1;	
						local sd_`group'=r(sd);
						local sd_`group' =substr("`sd_`group''", 1,5);
						local sd_`group' ="`sd_`group''";
						dis "sd_`group'";
					};						
					*p-values;
						reg `var' CONTROL if ICS==0, cluster(communityid);
						local p_5 = round((2 * ttail(e(df_r), abs(_b[CONTROL]/_se[CONTROL]))),.001);
						local p_5=substr("`p_5'",1,4);
						reg `var' CONTROL if STORAGE==0,cluster(communityid);
						local p_6 = round((2 * ttail(e(df_r), abs(_b[CONTROL]/_se[CONTROL]))),.001);
						local p_6=substr("`p_6'",1,4);				
						reg `var' STORAGE if CONTROL==0,cluster(communityid);
						local p_7= round((2 * ttail(e(df_r), abs(_b[STORAGE]/_se[STORAGE]))),.001);
						local p_7=substr("`p_7'",1,4);
					*Number of non-missing observations for that variable;
						count if `var'!=.;
						return list;
						local N_nm=r(N);
					*Fill table row for that column;
						preserve;
							use `sum_stats', clear;
							replace v1="`var_label'" if _n==`j';
							replace v2="`mean_CONTROL'" if _n==`j';
							replace v2="(`sd_CONTROL')" if _n==`k';
							replace v3="`mean_STORAGE'" if _n==`j';
							replace v3="(`sd_STORAGE')" if _n==`k';
							replace v4="`mean_ICS'" if _n==`j';
							replace v4="(`sd_ICS')" if _n==`k';
							forvalues i=5/7{;
								replace v`i'="`p_`i''" if _n==`j' ;
								replace v`i'="`p_`i''*" if _n==`j' & `p_`i''<.1;
								replace v`i'="`p_`i''**" if _n==`j' & `p_`i''<.05;
								replace v`i'="`p_`i''***" if _n==`j' & `p_`i''<.01;
							};
							replace v8="`N_nm'" if _n==`j';
							sa `sum_stats', replace;
						restore;
						local j=`j'+2;
				};		
			*Export summary stat table to latex;				
				use `sum_stats', clear;
				des, fu;	
				drop if v2=="";
				listtex using tex/t_balancing_`tab_date'.tex, rstyle(tabular) replace
								headlines( "&&&Inventory&&&&\\" 
								"&Control&Storage&Credit&P-value&P-value&P-value&\\" 
											"&[C]&[S]&[I]&[C-S]&[C-I]&[S-I]&N\\" "\hline" "&&&&&&&\\")
							footlines("\bottomrule" );							
		};


